package com.qf.controller;

import com.qf.dto.RankDto;
import com.qf.dto.ShowDto;
import com.qf.dto.UserDto;
import com.qf.entity.Comment;
import com.qf.entity.User;
import com.qf.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @version 1.0
 * @date 2019/10/24 8:58
 */
@Controller
public class CommentController {
    @Resource
    private LoveRankService rankService;

    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;
    @Resource
    private LikeService likeService;

    @Resource
    private LoveShowService showService;

    @RequestMapping("/community/getCommunitys")
    @ResponseBody
    public Map getCommunitys(@RequestParam(defaultValue = "1") Integer pageIndex) {
        Map map = new HashMap();
        List list = commentService.getCommunitys(pageIndex);
        map.put("code", 1);
        map.put("showList", list);
        return map;
    }

    @RequestMapping("/community/likeIt")
    @ResponseBody
    public Map likeIt(Integer showId, HttpSession session) {
        Map map = new HashMap();
        int i = likeService.likeIt(showId, 2);

        map.put("code", i);
        if (i > 0) {
            map.put("msg", "点赞成功");
        } else {
            map.put("msg", "点赞取消");
        }
        return map;
    }

    @RequestMapping("/community/getComments")
    @ResponseBody
    public Map getComments(Integer showId) {
        Map map = new HashMap();
        ShowDto showDto = showService.getShowByShowId(showId);
        List<Comment> commentList = commentService.getComments(showId);

        map.put("code", 1);
        map.put("msg", "success");
        map.put("commentCount", showDto.getCommentCount());
        map.put("show", showDto);
        map.put("commentList", commentList);
        return map;
    }

    @RequestMapping("/comment/addComment")
    @ResponseBody
    public Map addComment(String commentText, Integer showId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        int i = commentService.addComment(commentText, showId, 2);
        Map map = new HashMap();
        if (i > 0) {
            map.put("code", 1);
            map.put("msg", "success");

        } else {
            map.put("code", 0);
            map.put("msg", "fail");
        }
        return map;

    }

    @RequestMapping("/comment/getPersonalPage")
    @ResponseBody
    public Map getPersonalPage(Integer userId){
        UserDto userDto = userService.findUserByUserId(userId);
        Map map = new HashMap();
        map.put("code", 1);
        map.put("msg", "success");
        map.put("user", userDto);
        return map;
    }

    @RequestMapping("/comment/getLoveRank")
    @ResponseBody
    public Map getLoveRank(){
        List<RankDto> loverankList = rankService.getLoveRank();
        Map map = new HashMap();
        map.put("code", 1);
        map.put("msg", "success");
        map.put("rankList", loverankList);
        return map;
    }






}
